home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / expr.c < prev    next >
C/C++ Source or Header  |  1993-12-28  |  18KB  |  892 lines

  1. /* expr.c -- arithmetic expression evaluation. */
  2.  
  3. /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT
  13.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  15.    License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with Bash; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. /*
  22.  All arithmetic is done as long integers with no checking for overflow
  23.  (though division by 0 is caught and flagged as an error).
  24.  
  25.  The following operators are handled, grouped into a set of levels in
  26.  order of decreasing precedence.
  27.  
  28.     "-", "+"        [(unary operators)]
  29.     "!", "~"
  30.     "*", "/", "%"
  31.     "+", "-"
  32.     "<<", ">>"
  33.     "<=", ">=", "<", ">"
  34.     "==", "!="
  35.     "&"
  36.     "^"
  37.     "|"
  38.     "&&"
  39.     "||"
  40.     "="
  41.  
  42.  (Note that most of these operators have special meaning to bash, and an
  43.  entire expression should be quoted, e.g. "a=$a+1" or "a=a+1" to ensure
  44.  that it is passed intact to the evaluator when using `let'.  When using
  45.  the $[] or $(( )) forms, the text between the `[' and `]' or `((' and `))'
  46.  is treated as if in double quotes.)
  47.  
  48.  Sub-expressions within parentheses have a precedence level greater than
  49.  all of the above levels and are evaluated first.  Within a single prece-
  50.  dence group, evaluation is left-to-right, except for the arithmetic
  51.  assignment operator (`='), which is evaluated right-to-left (as in C).
  52.  
  53.  The expression evaluator returns the value of the expression (assignment
  54.  statements have as a value what is returned by the RHS).  The `let'
  55.  builtin, on the other hand, returns 0 if the last expression evaluates to
  56.  a non-zero, and 1 otherwise.
  57.  
  58.  Implementation is a recursive-descent parser.
  59.  
  60.  Chet Ramey
  61.  chet@ins.CWRU.Edu
  62. */
  63.  
  64. #include <stdio.h>
  65. #include "bashansi.h"
  66. #include "shell.h"
  67.  
  68. #define variable_starter(c) (isletter(c) || (c == '_'))
  69. #define variable_character(c) (isletter(c) || (c == '_') || digit(c))
  70.  
  71. /* Because of the $((...)) construct, expressions may include newlines.
  72.    Here is a macro which accepts newlines, tabs and spaces as whitespace. */
  73. #define cr_whitespace(c) (whitespace(c) || ((c) == '\n'))
  74.  
  75. static char    *expression = (char *) NULL;    /* The current expression */
  76. static char    *tp = (char *) NULL;        /* token lexical position */
  77. static int    curtok = 0;            /* the current token */
  78. static int    lasttok = 0;            /* the previous token */
  79. static int    assigntok = 0;            /* the OP in OP= */
  80. static char    *tokstr = (char *) NULL;    /* current token string */
  81. static int    tokval = 0;            /* current token value */
  82. static jmp_buf    evalbuf;
  83.  
  84. static void    readtok ();            /* lexical analyzer */
  85. static long    expassign (), exp0 (), exp1 (), exp2 (), exp3 (),
  86.         exp4 (), exp5 (), expshift (), expland (), explor (),
  87.         expband (), expbor (), expbxor ();
  88. static long    strlong ();
  89. static void    evalerror ();
  90.  
  91. /* A structure defining a single expression context. */
  92. typedef struct {
  93.   int curtok, lasttok;
  94.   char *expression, *tp;
  95.   int tokval;
  96.   char *tokstr;
  97. } EXPR_CONTEXT;
  98.  
  99. /* Global var which contains the stack of expression contexts. */
  100. static EXPR_CONTEXT **expr_stack;
  101. static int expr_depth = 0;       /* Location in the stack. */
  102. static int expr_stack_size = 0;       /* Number of slots already allocated. */
  103.  
  104. /* Size be which the expression stack grows when neccessary. */
  105. #define EXPR_STACK_GROW_SIZE 10
  106.  
  107. /* Maximum amount of recursion allowed.  This prevents a non-integer
  108.    variable such as "num=num+2" from infinitely adding to itself when
  109.    "let num=num+2" is given.  I have to talk to Chet about this hack. */
  110. #define MAX_EXPR_RECURSION_LEVEL 1024
  111.  
  112. /* The Tokens.  Singing "The Lion Sleeps Tonight". */
  113.  
  114. #define EQEQ    1    /* "==" */
  115. #define NEQ    2    /* "!=" */
  116. #define LEQ    3    /* "<=" */
  117. #define GEQ    4    /* ">=" */
  118. #define STR    5    /* string */
  119. #define NUM    6    /* number */
  120. #define LAND    7    /* "&&" Logical AND */
  121. #define LOR    8    /* "||" Logical OR */
  122. #define LSH    9    /* "<<" Left SHift */
  123. #define RSH    10    /* ">>" Right SHift */
  124. #define OP_ASSIGN 11    /* op= expassign as in Posix.2 */
  125. #define EQ    '='
  126. #define GT    '>'
  127. #define LT    '<'
  128. #define PLUS    '+'
  129. #define MINUS    '-'
  130. #define MUL    '*'
  131. #define DIV    '/'
  132. #define MOD    '%'
  133. #define NOT    '!'
  134. #define LPAR    '('
  135. #define RPAR    ')'
  136. #define BAND    '&'    /* Bitwise AND */
  137. #define BOR    '|'    /* Either Bitwise OR, or what Chet is. */
  138. #define BXOR    '^'    /* Bitwise eXclusive OR. */
  139. #define BNOT    '~'    /* Bitwise NOT; Two's complement. */
  140.  
  141. /* Push and save away the contents of the globals describing the
  142.    current expression context. */
  143. static void
  144. pushexp ()
  145. {
  146.   EXPR_CONTEXT *context;
  147.  
  148.   context = (EXPR_CONTEXT *)xmalloc (sizeof (EXPR_CONTEXT));
  149.  
  150.   if (expr_depth >= MAX_EXPR_RECURSION_LEVEL)
  151.     evalerror ("expression recursion level exceeded");
  152.  
  153.   if (expr_depth >= expr_stack_size)
  154.     {
  155.       expr_stack = (EXPR_CONTEXT **)
  156.     xrealloc (expr_stack, (expr_stack_size += EXPR_STACK_GROW_SIZE)
  157.           * sizeof (EXPR_CONTEXT *));
  158.     }
  159.  
  160.   context->curtok = curtok;
  161.   context->lasttok = lasttok;
  162.   context->expression = expression;
  163.   context->tp = tp;
  164.   context->tokval = tokval;
  165.   context->tokstr = tokstr;
  166.   expr_stack[expr_depth++] = context;
  167. }
  168.  
  169. /* Pop the the contents of the expression context stack into the
  170.    globals describing the current expression context. */
  171. static void
  172. popexp ()
  173. {
  174.   EXPR_CONTEXT *context;
  175.  
  176.   if (expr_depth == 0)
  177.     evalerror ("Recursion stack underflow");
  178.  
  179.   context = expr_stack[--expr_depth];
  180.   curtok = context->curtok;
  181.   lasttok = context->lasttok;
  182.   expression = context->expression;
  183.   tp = context->tp;
  184.   tokval = context->tokval;
  185.   tokstr = context->tokstr;
  186.   free (context);
  187. }
  188.  
  189. /* Evaluate EXPR, and return the arithmetic result.
  190.  
  191.    The `while' loop after the longjmp is caught relies on the above
  192.    implementation of pushexp and popexp leaving in expr_stack[0] the
  193.    values that the variables had when the program started.  That is,
  194.    the first things saved are the initial values of the variables that 
  195.    were assigned at program startup or by the compiler.  Therefore, it is
  196.    safe to let the loop terminate when expr_depth == 0, without freeing up
  197.    any of the expr_depth[0] stuff. */
  198. long
  199. evalexp (expr)
  200.      char *expr;
  201. {
  202.   long val = 0L;
  203.   jmp_buf old_evalbuf;
  204.   char *p;
  205.  
  206.   for (p = expr; p && *p && cr_whitespace (*p); p++)
  207.     ;
  208.  
  209.   if (p == NULL || *p == '\0')
  210.     return (0);
  211.  
  212.   /* Save the value of evalbuf to protect it around possible recursive
  213.      calls to evalexp (). */
  214.   xbcopy ((char *)evalbuf, (char *)old_evalbuf, sizeof (jmp_buf));
  215.  
  216.   if (setjmp (evalbuf))
  217.     {
  218.       if (tokstr)        /* Clean up local allocation. */
  219.     free (tokstr);
  220.  
  221.       if (expression)
  222.     free (expression);
  223.  
  224.       while (--expr_depth)
  225.     {
  226.       if (expr_stack[expr_depth]->tokstr)
  227.         free (expr_stack[expr_depth]->tokstr);
  228.  
  229.       if (expr_stack[expr_depth]->expression)
  230.         free (expr_stack[expr_depth]->expression);
  231.     }
  232.       longjmp (top_level, DISCARD);
  233.     }
  234.  
  235.   pushexp ();
  236.   curtok = lasttok = 0;
  237.   expression = savestring (expr);
  238.   tp = expression;
  239.  
  240.   tokstr = (char *)NULL;
  241.   tokval = 0l;
  242.  
  243.   readtok ();
  244.  
  245.   val = expassign ();
  246.  
  247.   if (curtok != 0) 
  248.     evalerror ("syntax error in expression");
  249.  
  250.   if (tokstr)
  251.     free (tokstr);
  252.   if (expression)
  253.     free (expression);
  254.  
  255.   popexp ();
  256.  
  257.   /* Restore the value of evalbuf so that any subsequent longjmp calls
  258.      will have a valid location to jump to. */
  259.   xbcopy ((char *)old_evalbuf, (char *)evalbuf, sizeof (jmp_buf));
  260.  
  261.   return (val);
  262. }
  263.  
  264. /* Bind/create a shell variable with the name LHS to the RHS.
  265.    This creates or modifies a variable such that it is an integer.
  266.  
  267.    This should really be in variables.c, but it is here so that all of the
  268.    expression evaluation stuff is localized.  Since we don't want any
  269.    recursive evaluation from bind_variable() (possible without this code,
  270.    since bind_variable() calls the evaluator for variables with the integer
  271.    attribute set), we temporarily turn off the integer attribute for each
  272.    variable we set here, then turn it back on after binding as necessary. */
  273.  
  274. void
  275. bind_int_variable (lhs, rhs)
  276.      char *lhs, *rhs;
  277. {
  278.   register SHELL_VAR *v;
  279.   int isint = 0;
  280.  
  281.   v = find_variable (lhs);
  282.   if (v)
  283.     {
  284.       isint = integer_p (v);
  285.       v->attributes &= ~att_integer;
  286.     }
  287.  
  288.   v = bind_variable (lhs, rhs);
  289.   if (isint)
  290.     v->attributes |= att_integer;
  291. }
  292.  
  293. static long
  294. expassign ()
  295. {
  296.   register long    value;
  297.   char *lhs, *rhs;
  298.  
  299.   value = explor ();
  300.   if (curtok == EQ || curtok == OP_ASSIGN)
  301.     {
  302.       int special = curtok == OP_ASSIGN;
  303.       int op;
  304.       long lvalue;
  305.  
  306.       if (lasttok != STR)
  307.     evalerror ("attempted expassign to non-variable");
  308.  
  309.       if (special)
  310.     {
  311.       op = assigntok;        /* a OP= b */
  312.       lvalue = value;
  313.     }
  314.  
  315.       lhs = savestring (tokstr);
  316.       readtok ();
  317.       value = expassign ();
  318.  
  319.       if (special)
  320.     {
  321.       switch (op)
  322.         {
  323.         case MUL:
  324.           lvalue *= value;
  325.           break;
  326.         case DIV:
  327.           lvalue /= value;
  328.           break;
  329.         case MOD:
  330.           lvalue %= value;
  331.           break;
  332.         case PLUS:
  333.           lvalue += value;
  334.           break;
  335.         case MINUS:
  336.           lvalue -= value;
  337.           break;
  338.         case LSH:
  339.           lvalue <<= value;
  340.           break;
  341.         case RSH:
  342.           lvalue >>= value;
  343.           break;
  344.         case BAND:
  345.           lvalue &= value;
  346.           break;
  347.         case BOR:
  348.           lvalue |= value;
  349.           break;
  350.         default:
  351.           evalerror ("bug: bad expassign token %d", assigntok);
  352.           break;
  353.         }
  354.       value = lvalue;
  355.     }
  356.  
  357.       rhs = itos (value);
  358.       bind_int_variable (lhs, rhs);
  359.       free (rhs);
  360.       free (lhs);
  361.       free (tokstr);
  362.       tokstr = (char *)NULL;        /* For freeing on errors. */
  363.     }
  364.   return (value);
  365. }
  366.  
  367. /* Logical OR. */
  368. static long
  369. explor ()
  370. {
  371.   register long val1, val2;
  372.  
  373.   val1 = expland ();
  374.  
  375.   while (curtok == LOR)
  376.     {
  377.       readtok ();
  378.       val2 = expland ();
  379.       val1 = val1 || val2;
  380.     }
  381.  
  382.   return (val1);
  383. }
  384.  
  385. /* Logical AND. */
  386. static long
  387. expland ()
  388. {
  389.   register long val1, val2;
  390.  
  391.   val1 = expbor ();
  392.  
  393.   while (curtok == LAND)
  394.     {
  395.       readtok ();
  396.       val2 = expbor ();
  397.       val1 = val1 && val2;
  398.     }
  399.  
  400.   return (val1);
  401. }
  402.  
  403. /* Bitwise OR. */
  404. static long
  405. expbor ()
  406. {
  407.   register long val1, val2;
  408.  
  409.   val1 = expbxor ();
  410.  
  411.   while (curtok == BOR)
  412.     {
  413.       readtok ();
  414.       val2 = expbxor ();
  415.       val1 = val1 | val2;
  416.     }
  417.  
  418.   return (val1);
  419. }
  420.  
  421. /* Bitwise XOR. */
  422. static long
  423. expbxor ()
  424. {
  425.   register long val1, val2;
  426.  
  427.   val1 = expband ();
  428.  
  429.   while (curtok == BXOR)
  430.     {
  431.       readtok ();
  432.       val2 = expband ();
  433.       val1 = val1 ^ val2;
  434.     }
  435.  
  436.   return (val1);
  437. }
  438.  
  439. /* Bitwise AND. */
  440. static long
  441. expband ()
  442. {
  443.   register long val1, val2;
  444.  
  445.   val1 = exp5 ();
  446.  
  447.   while (curtok == BAND)
  448.     {
  449.       readtok ();
  450.       val2 = exp5 ();
  451.       val1 = val1 & val2;
  452.     }
  453.  
  454.   return (val1);
  455. }
  456.  
  457. static long
  458. exp5 ()
  459. {
  460.   register long val1, val2;
  461.  
  462.   val1 = exp4 ();
  463.  
  464.   while ((curtok == EQEQ) || (curtok == NEQ))
  465.     {
  466.       int op = curtok;
  467.  
  468.       readtok ();
  469.       val2 = exp4 ();
  470.       if (op == EQEQ)
  471.     val1 = (val1 == val2);
  472.       else if (op == NEQ)
  473.     val1 = (val1 != val2);
  474.     }
  475.   return (val1);
  476. }
  477.  
  478. static long
  479. exp4 ()
  480. {
  481.   register long val1, val2;
  482.  
  483.   val1 = expshift ();
  484.   while ((curtok == LEQ) ||
  485.      (curtok == GEQ) ||
  486.      (curtok == LT) ||
  487.      (curtok == GT))
  488.     {
  489.       int op = curtok;
  490.  
  491.       readtok ();
  492.       val2 = expshift ();
  493.  
  494.       if (op == LEQ)
  495.     val1 = val1 <= val2;
  496.       else if (op == GEQ)
  497.     val1 = val1 >= val2;
  498.       else if (op == LT)
  499.     val1 = val1 < val2;
  500.       else if (op == GT)
  501.     val1 = val1 > val2;
  502.     }
  503.   return (val1);
  504. }
  505.  
  506. /* Left and right shifts. */
  507. static long
  508. expshift ()
  509. {
  510.   register long val1, val2;
  511.  
  512.   val1 = exp3 ();
  513.  
  514.   while ((curtok == LSH) || (curtok == RSH))
  515.     {
  516.       int op = curtok;
  517.  
  518.       readtok ();
  519.       val2 = exp3 ();
  520.  
  521.       if (op == LSH)
  522.     val1 = val1 << val2;
  523.       else
  524.     val1 = val1 >> val2;
  525.     }
  526.  
  527.   return (val1);
  528. }
  529.  
  530. static long
  531. exp3 ()
  532. {
  533.   register long val1, val2;
  534.  
  535.   val1 = exp2 ();
  536.  
  537.   while ((curtok == PLUS) || (curtok == MINUS))
  538.     {
  539.       int op = curtok;
  540.  
  541.       readtok ();
  542.       val2 = exp2 ();
  543.  
  544.       if (op == PLUS)
  545.     val1 += val2;
  546.       else if (op == MINUS)
  547.     val1 -= val2;
  548.     }
  549.   return (val1);
  550. }
  551.  
  552. static long
  553. exp2 ()
  554. {
  555.   register long val1, val2;
  556.  
  557.   val1 = exp1 ();
  558.  
  559.   while ((curtok == MUL) ||
  560.          (curtok == DIV) ||
  561.          (curtok == MOD))
  562.     {
  563.       int op = curtok;
  564.  
  565.       readtok ();
  566.  
  567.       val2 = exp1 ();
  568.  
  569.       if (((op == DIV) || (op == MOD)) && (val2 == 0))
  570.     evalerror ("division by 0");
  571.  
  572.       if (op == MUL)
  573.         val1 *= val2;
  574.       else if (op == DIV)
  575.         val1 /= val2;
  576.       else if (op == MOD)
  577.         val1 %= val2;
  578.     }
  579.   return (val1);
  580. }
  581.  
  582. static long
  583. exp1 ()
  584. {
  585.   register long val;
  586.  
  587.   if (curtok == NOT)
  588.     {
  589.       readtok ();
  590.       val = !exp1 ();
  591.     }
  592.   else if (curtok == BNOT)
  593.     {
  594.       readtok ();
  595.       val = ~exp1 ();
  596.     }
  597.   else
  598.     val = exp0 ();
  599.  
  600.   return (val);
  601. }
  602.  
  603. static long
  604. exp0 ()
  605. {
  606.   register long val = 0L;
  607.  
  608.   if (curtok == MINUS)
  609.     {
  610.       readtok ();
  611.       val = - exp0 ();
  612.     }
  613.   else if (curtok == PLUS)
  614.     {
  615.       readtok ();
  616.       val = exp0 ();
  617.     }
  618.   else if (curtok == LPAR)
  619.     {
  620.       readtok ();
  621.       val = expassign ();
  622.  
  623.       if (curtok != RPAR)
  624.     evalerror ("missing `)'");
  625.  
  626.       /* Skip over closing paren. */
  627.       readtok ();
  628.     }
  629.   else if ((curtok == NUM) || (curtok == STR))
  630.     {
  631.       val = tokval;
  632.       readtok ();
  633.     }
  634.   else
  635.     evalerror ("syntax error in expression");
  636.  
  637.   return (val);
  638. }
  639.  
  640. /* Lexical analyzer/token reader for the expression evaluator.  Reads the
  641.    next token and puts its value into curtok, while advancing past it.
  642.    Updates value of tp.  May also set tokval (for number) or tokstr (for
  643.    string). */
  644. static void
  645. readtok ()
  646. {
  647.   register char *cp = tp;
  648.   register int c, c1;
  649.  
  650.   /* Skip leading whitespace. */
  651.   c = 0;
  652.   while (cp && (c = *cp) && (cr_whitespace (c)))
  653.     cp++;
  654.  
  655.   if (c)
  656.     cp++;
  657.     
  658.   tp = cp - 1;
  659.  
  660.   if (c == '\0')
  661.     {
  662.       lasttok = curtok;
  663.       curtok = 0;
  664.       tp = cp;
  665.       return;
  666.     }
  667.  
  668.   if (variable_starter (c))
  669.     {
  670.       /* Semi-bogus K*rn shell compatibility feature -- variable
  671.      names not preceded with a dollar sign are shell variables. */
  672.       char *value;
  673.  
  674.       while (variable_character (c))
  675.     c = *cp++;
  676.  
  677.       c = *--cp;
  678.       *cp = '\0';
  679.  
  680.       if (tokstr)
  681.         free (tokstr);
  682.       tokstr = savestring (tp);
  683.       value = get_string_value (tokstr);
  684.  
  685.       if (value && *value)
  686.     tokval = evalexp (value);
  687.       else
  688.     tokval = 0;
  689.  
  690.       *cp = c;
  691.       lasttok = curtok;
  692.       curtok = STR;
  693.     }
  694.   else if (digit(c))
  695.     {
  696.       while (digit (c) || isletter (c) || c == '#')
  697.     c = *cp++;
  698.  
  699.       c = *--cp;
  700.       *cp = '\0';
  701.  
  702.       tokval = strlong (tp);
  703.       *cp = c;
  704.       lasttok = curtok;
  705.       curtok = NUM;
  706.     }
  707.   else
  708.     {
  709.       c1 = *cp++;
  710.       if ((c == EQ) && (c1 == EQ)) 
  711.     c = EQEQ;
  712.       else if ((c == NOT) && (c1 == EQ))
  713.     c = NEQ;
  714.       else if ((c == GT) && (c1 == EQ))
  715.     c = GEQ;
  716.       else if ((c == LT) && (c1 == EQ))
  717.     c = LEQ;
  718.       else if ((c == LT) && (c1 == LT))
  719.     {
  720.       if (*cp == '=')    /* a <<= b */
  721.         {
  722.           assigntok = LSH;
  723.           c = OP_ASSIGN;
  724.           cp++;
  725.         }
  726.       else
  727.         c = LSH;
  728.     }
  729.       else if ((c == GT) && (c1 == GT))
  730.     {
  731.       if (*cp == '=')
  732.         {
  733.           assigntok = RSH;    /* a >>= b */
  734.           c = OP_ASSIGN;
  735.           cp++;
  736.         }
  737.       else
  738.         c = RSH;
  739.     }
  740.       else if ((c == BAND) && (c1 == BAND))
  741.     c = LAND;
  742.       else if ((c == BOR) && (c1 == BOR))
  743.     c = LOR;
  744.       else if (c1 == EQ && member(c, "*/%+-&^|"))
  745.     {
  746.       assigntok = c;    /* a OP= b */
  747.       c = OP_ASSIGN;
  748.     }
  749.       else
  750.     cp--;            /* `unget' the character */
  751.       lasttok = curtok;
  752.       curtok = c;
  753.     }
  754.   tp = cp;
  755. }
  756.  
  757. static void
  758. evalerror (msg)
  759.      char *msg;
  760. {
  761.   builtin_error ("%s: %s (remainder of expression is \"%s\")",
  762.          expression, msg, (tp && *tp) ? tp : "");
  763.   longjmp (evalbuf, 1);
  764. }
  765.  
  766. /* Convert a string to a long integer, with an arbitrary base.
  767.    0nnn -> base 8
  768.    0xnn -> base 16
  769.    Anything else: [base#]number (this is from the ISO Pascal spec). */
  770. static long
  771. strlong (num)
  772.      char *num;
  773. {
  774.   register char *s = num;
  775.   register int c;
  776.   int base = 10;
  777.   long val = 0L;
  778.  
  779.   if (s == NULL || *s == '\0')
  780.     return 0L;
  781.  
  782.   if (*s == '0')
  783.     {
  784.       s++;
  785.  
  786.       if (s == NULL || *s == '\0')
  787.     return 0L;
  788.       
  789.        /* Base 16? */
  790.       if (*s == 'x' || *s == 'X')
  791.     {
  792.       base = 16;
  793.       s++;
  794.     }
  795.       else
  796.     base = 8;
  797.     }
  798.  
  799.   for (c = *s++; c; c = *s++)
  800.     {
  801.       if (c == '#')
  802.     {
  803.       base = (int)val;
  804.  
  805.       /* Illegal base specifications are silently reset to base 10.
  806.          I don't think that this is a good idea? */
  807.       if (base < 2 || base > 36)
  808.         base = 10;
  809.  
  810.       val = 0L;
  811.     }
  812.       else
  813.     if (isletter(c) || digit(c))
  814.       {
  815.         if (digit(c))
  816.           c = digit_value(c);
  817.         else if (c >= 'a' && c <= 'z')
  818.           c -= 'a' - 10;
  819.         else if (c >= 'A' && c <= 'Z')
  820.           c -= 'A' - 10;
  821.  
  822.         if (c >= base)
  823.           evalerror ("value too great for base");
  824.  
  825.         val = (val * base) + c;
  826.       }
  827.     else
  828.       break;
  829.     }
  830.   return (val);
  831. }
  832.  
  833. #if defined (EXPR_TEST)
  834. char *
  835. xmalloc (n)
  836.      int n;
  837. {
  838.   return (malloc (n));
  839. }
  840.  
  841. char *
  842. xrealloc (s, n)
  843.      char *s;
  844.      int n;
  845. {
  846.   return (realloc (s, n));
  847. }
  848.  
  849. SHELL_VAR *find_variable () { return 0;}
  850. SHELL_VAR *bind_variable () { return 0; }
  851.  
  852. char *get_string_value () { return 0; }
  853.  
  854. jmp_buf top_level;
  855.  
  856. main (argc, argv)
  857.      int argc;
  858.      char **argv;
  859. {
  860.   register int i;
  861.   long v;
  862.  
  863.   if (setjmp (top_level))
  864.     exit (0);
  865.  
  866.   for (i = 1; i < argc; i++)
  867.     {
  868.       v = evalexp (argv[i]);
  869.       printf ("'%s' -> %ld\n", argv[i], v);
  870.     }
  871.   exit (0);
  872. }
  873.  
  874. int
  875. builtin_error (format, arg1, arg2, arg3, arg4, arg5)
  876.      char *format;
  877. {
  878.   fprintf (stderr, "expr: ");
  879.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  880.   fprintf (stderr, "\n");
  881.   return 0;
  882. }
  883.  
  884. char *
  885. itos (n)
  886.      int n;
  887. {
  888.   return ("42");
  889. }
  890.  
  891. #endif /* EXPR_TEST */
  892.